package com.guigu.service.impl.stock;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.mapper.stock.SGatherMapper;
import com.guigu.pojo.stock.SGather;
import com.guigu.pojo.stock.SPay;
import com.guigu.service.stock.SGatherService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr.Zhang
 * @since 2024-12-06
 */
@Service
public class SGatherServiceImpl extends ServiceImpl<SGatherMapper, SGather> implements SGatherService {

    @Autowired
    private SGatherMapper sGatherMapper;

    @Override
    public SGather getEndSGather() {
        QueryWrapper<SGather> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("GATHER_ID");
        queryWrapper.last("limit 1");
        SGather sGather = getOne(queryWrapper);
        return sGather;
    }

    @Override
    public int addSGather(SGather sGather) {
        int insert = sGatherMapper.insert(sGather);
        return sGather.getId();
    }

    @Override
    public PageInfo<SGather> selectSGatherByPage(HashMap<String, Object> map) {
        Integer pageNum = 1;
        Integer pageSize = 5;

        Object pageNumObj = map.get("pageNum");
        if (pageNumObj != null && StringUtils.isNotBlank((String) pageNumObj)) {
            pageNum = Integer.parseInt((String) pageNumObj);
        }
        Object pageSizeObj = map.get("pageSize");
        if (pageSizeObj != null && StringUtils.isNotBlank((String) pageSizeObj)) {
            pageSize = Integer.parseInt((String) pageSizeObj);
        }
        QueryWrapper<SGather> queryWrapper = new QueryWrapper<>();

        if (map.containsKey("checkTag")){
            queryWrapper.eq("CHECK_TAG",map.get("checkTag"));
        }
        if(map.containsKey("gatherTag")){
            queryWrapper.eq("GATHER_TAG",map.get("gatherTag"));
        }
        if(map.containsKey("gatherCheckTag")){
            queryWrapper.eq("GATHER_CHECK_TAG",map.get("gatherCheckTag"));
        }
        PageHelper.startPage(pageNum,pageSize);
        List<SGather> sGathers = sGatherMapper.selectList(queryWrapper);
        PageInfo<SGather> pageInfo = new PageInfo<>(sGathers);
        return pageInfo;
    }

    @Override
    public boolean updateSGather(SGather sGather) {
        QueryWrapper<SGather> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("GATHER_ID", sGather.getGatherId());
        int update = sGatherMapper.update(sGather,queryWrapper);
        return update>0;

    }

    @Override
    public Integer findBySGatherId(String sGatherId) {
        QueryWrapper<SGather> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("GATHER_ID", sGatherId);
        SGather sGather = sGatherMapper.selectOne(queryWrapper);

        return sGather.getId();
    }
}
